package itqf.controller;



import itqf.entity.GoodTypes;
import itqf.entity.Product;
import itqf.service.GoodTypesServiceImpl;
import itqf.service.ProductServiceImpl;
import itqf.service.impl.GoodTypesService;
import itqf.service.impl.ProductService;
import itqf.utils.PageBean;
import itqf.utils.SysConstant;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ${PACKAGE_NAME}
 * 2020/9/15
 */
@WebServlet(name = "goodsController",value = "/goodsController")
public class goodsController extends BaseController {
    GoodTypesService goodTypesService = new GoodTypesServiceImpl();
    ProductService productService = new ProductServiceImpl();


    public List<GoodTypes> getAllType(HttpServletRequest request, HttpServletResponse response){
        return goodTypesService.findAllGoodTypes();
    }

    public String getGoodsByTypeId(HttpServletRequest request, HttpServletResponse response){
        String typeId = request.getParameter("typeId");
        int tid = Integer.parseInt(typeId);
        String nowPageStr = request.getParameter("nowPage");
        int nowPage ;
        int pageSize = 5;
        if (nowPageStr==null){
            nowPage = 1;
        }else{
            nowPage = Integer.parseInt(nowPageStr);
        }

        int startIndex = (nowPage-1)*5;
        PageBean bean =  productService.findProductByTypeId(tid,startIndex,pageSize);
        bean.setNowPage(nowPage);
        request.setAttribute("pageBean",bean);
        request.setAttribute("typeId",typeId);
        bean.cal();
        return SysConstant.FORWARD+SysConstant.FALG+"goodsList.jsp";

    }

    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        Product p = productService.finProductDetailById(id);
        request.setAttribute("goods",p);
        return SysConstant.FORWARD+SysConstant.FALG+"goodsDetail.jsp";

    }

    public String  showGoodsType(HttpServletRequest request, HttpServletResponse response){
        List<Product> products = productService.showGoodsType();
        request.setAttribute("products",products);
        return SysConstant.FORWARD+SysConstant.FALG+"admin/showGoodsType.jsp";
    }

    public String  listGoodsType(HttpServletRequest request, HttpServletResponse response){
        List<GoodTypes> list = goodTypesService.findAllGoodTypes();
        request.setAttribute("goodsTypeList",list);
        return SysConstant.FORWARD+SysConstant.FALG+"admin/addGoodsType.jsp";
        //return list;
    }
    public String addGoodsType(HttpServletRequest request, HttpServletResponse response){
        String tname = request.getParameter("typename");
        goodTypesService.insertGoodsType(tname);
        return listGoodsType(request,response);
    }

    public String  addGoods(HttpServletRequest request, HttpServletResponse response){
        List<Product> products = productService.showAllProduct();
        request.setAttribute("products",products);
        return SysConstant.FORWARD+SysConstant.FALG+"admin/showGoods.jsp";
    }

}
